package study.mybatis.database.cpool.support.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import study.mybatis.database.cpool.mapper.UserBaseEO;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

/**
 *
 */
@Slf4j
@Configuration
public class DataSourceConfig {

    @Autowired
    private DataSource dataSource;

    @PostConstruct
    public void init() {
        log.info(">>>>>>>>>> {}", dataSource.getClass().getSimpleName());
        if (dataSource instanceof DruidDataSourceWrapper) {
            DruidDataSourceWrapper ds = (DruidDataSourceWrapper) dataSource;
            log.info("initial-size= {}", ds.getInitialSize());
            log.info("min-idle= {}", ds.getMinIdle());
            log.info("max-active= {}", ds.getMaxActive());
            log.info("max-wait= {}", ds.getMaxWait());
            log.info("use-unfair-lock= {}", ds.isUseUnfairLock());
            log.info("TimeBetweenEvictionRunsMillis= {}", ds.getTimeBetweenEvictionRunsMillis());

            //log.info("min-evictable-idle-time-millis= {}", ds.getMinEvictableIdleTimeMillis());
            //log.info("max-evictable-idle-time-millis= {}", ds.getMaxEvictableIdleTimeMillis());
        } else if (dataSource instanceof HikariDataSource) {
            DruidDataSourceWrapper ds = (DruidDataSourceWrapper) dataSource;
        }
    }

    //    @ConditionalOnClass(HikariDataSource.class)
    @ConditionalOnProperty(name = "spring.datasource.type",
            havingValue = "com.zaxxer.hikari.HikariDataSource")
    @Bean
    public UserBaseEO tt() {
        log.info("阿萨法水电费");
        return new UserBaseEO();
    }
}
